package uk.ac.salfordcc.sccmobile;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by bwarrington on 25/07/13.
 */
public class StoreCredentials  extends Activity{
    EditText mUsername, mPassword, mPin;
    Button mSave;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_credentials);

        ActionBar bar = getActionBar();
        bar.hide();

        mUsername = (EditText)findViewById(R.id.scc_username);
        mPassword = (EditText)findViewById(R.id.scc_password);
        mSave = (Button)findViewById(R.id.saveCredentials);
        mPin = (EditText)findViewById(R.id.scc_pin);

        loadPreferences();
    }

    private void loadPreferences(){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String storedUsername = sp.getString("USERNAME", ""); //returns "" if not stored.
        mUsername.setText(storedUsername);

        Log.e("sccUSERNAME:", mUsername.getText().toString());
    }

    private void savePreferences(String key, String value){
        String logString = "scc: [Save Preferences] SaveKey = " + key + " || SaveValue = " + value;
        Log.e(logString, "");

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        Editor editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
        Log.e("scc: Preferences Saved", "");

        loadPreferences();
    }

    public void saveCredentials(View view) {
        // TODO Auto-generated method stub
        if(mUsername.getText().toString().matches("")){
            Toast.makeText(this, "Enter username", Toast.LENGTH_SHORT).show();
        }
        else{
            if(mPassword.getText().toString().matches("")){
                Toast.makeText(this, "Enter password", Toast.LENGTH_SHORT).show();
            }
            else{
                //checkPINStatus();
                savePreferences("USERNAME", mUsername.getText().toString());
                savePreferences("PASSWORD", mPassword.getText().toString());
                savePreferences("PIN_SEC", mPin.getText().toString());
                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                Editor editor = sp.edit();
                editor.putLong("PIN_EXP", System.currentTimeMillis()+360000).commit();
                finish();
            }
        }
    }


    public void checkPINStatus(){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String pin = sp.getString("LOCK_PIN", "");

        if(pin == null || pin.matches("")){
            Intent intent = new Intent(this, PINActivity.class);
            //startActivity(intent);
        }
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBackPressed(){
    	// Call the application exit function immediately.
    	//SharedUtilities.endApplication(this);
        this.finishAffinity();
    }
}
